/*
 * EBMeterstand.java
 *
 * Created on 2 juni 2008, 19:51
 */

package energiebedrijf;

import energiebedrijf.Classes.Perceel;
import org.jdesktop.application.Action;

/**
 * Voer de meterstanden in voor het geselecteerde perceel
 * @author  pdb
 */
public class EBMeterstand extends javax.swing.JDialog {
    private Perceel perceel;
    private boolean _cancel;
    
    /** Creates new form EBMeterstand */
    public EBMeterstand(java.awt.Frame parent, boolean modal, Perceel perceel) {
        super(parent, modal);
        initComponents();
        
        this.setLocationRelativeTo(parent);
        this.perceel = perceel;
        
        if (this.perceel.getElectriciteit() == null) {
            this.txtElecStand.setEnabled(false);
            this.txtElectraNachtstand.setEnabled(false);
        }
        
        if (this.perceel.getGas() == null) {
            this.txtGasStand.setEnabled(false);
        }
        
        if (this.perceel.getWater() == null) {
            this.txtWaterStand.setEnabled(false);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtGasStand = new javax.swing.JTextField();
        txtWaterStand = new javax.swing.JTextField();
        txtElecStand = new javax.swing.JTextField();
        txtElectraNachtstand = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        btnCancel = new javax.swing.JButton();
        btnSave = new javax.swing.JButton();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(energiebedrijf.EnergieBedrijfApp.class).getContext().getResourceMap(EBMeterstand.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.setName("jPanel1"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        txtGasStand.setText(resourceMap.getString("txtGasStand.text")); // NOI18N
        txtGasStand.setName("txtGasStand"); // NOI18N

        txtWaterStand.setText(resourceMap.getString("txtWaterStand.text")); // NOI18N
        txtWaterStand.setName("txtWaterStand"); // NOI18N

        txtElecStand.setText(resourceMap.getString("txtElecStand.text")); // NOI18N
        txtElecStand.setName("txtElecStand"); // NOI18N

        txtElectraNachtstand.setText(resourceMap.getString("txtElectraNachtstand.text")); // NOI18N
        txtElectraNachtstand.setName("txtElectraNachtstand"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtElecStand, javax.swing.GroupLayout.DEFAULT_SIZE, 208, Short.MAX_VALUE)
                    .addComponent(txtWaterStand, javax.swing.GroupLayout.DEFAULT_SIZE, 208, Short.MAX_VALUE)
                    .addComponent(txtGasStand, javax.swing.GroupLayout.DEFAULT_SIZE, 208, Short.MAX_VALUE)
                    .addComponent(txtElectraNachtstand, javax.swing.GroupLayout.DEFAULT_SIZE, 208, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtGasStand, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtWaterStand, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtElecStand, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(txtElectraNachtstand, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btnCancel.setText(resourceMap.getString("btnCancel.text")); // NOI18N
        btnCancel.setName("btnCancel"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(energiebedrijf.EnergieBedrijfApp.class).getContext().getActionMap(EBMeterstand.class, this);
        btnSave.setAction(actionMap.get("btnSave_Click")); // NOI18N
        btnSave.setText(resourceMap.getString("btnSave.text")); // NOI18N
        btnSave.setName("btnSave"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnSave)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCancel)))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnCancel, btnSave});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancel)
                    .addComponent(btnSave))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Slaat de wijzigingen op
     */
    @Action
    public void btnSave_Click() {
        this._cancel = false;
        
        if (this.getPerceel().getGas() != null) {
            int vorigestand = this.getPerceel().getGas().getVorigeMeterstand();
            int verbruik = 0;
            int invoer = Integer.parseInt(txtGasStand.getText());
            
            if (vorigestand == 0) {
                //stand = veerbruik
                verbruik = invoer;
            } else {
                //bereken verbruik
                verbruik = invoer - vorigestand;
            }
            this.getPerceel().getGas().setVorigeMeterstand(invoer);
            this.getPerceel().getGas().setVerbruik(verbruik);
        }
        
        if (this.getPerceel().getElectriciteit() != null) {
            int vorigestand = this.getPerceel().getElectriciteit().getVorigeMeterstand();
            int verbruik = 0;
            int invoer = Integer.parseInt(txtElecStand.getText());
            
            if (vorigestand == 0) {
                //stand = veerbruik
                verbruik = invoer;
            } else {
                //bereken verbruik
                verbruik = invoer - vorigestand;
            }
            this.getPerceel().getElectriciteit().setVorigeMeterstand(invoer);
            this.getPerceel().getElectriciteit().setVerbruik(verbruik);
            
            vorigestand = this.getPerceel().getElectriciteit().getVorigeMeterstandNacht();
            verbruik = 0;
            invoer = Integer.parseInt(txtElectraNachtstand.getText());
            
            if (vorigestand == 0) {
                //stand = veerbruik
                verbruik = invoer;
            } else {
                //bereken verbruik
                verbruik = invoer - vorigestand;
            }
            this.getPerceel().getElectriciteit().setVorigeMeterstandNacht(invoer);
            this.getPerceel().getElectriciteit().setVerbruikNacht(verbruik);
        }
        
        if (this.getPerceel().getWater() != null) {
            int vorigestand = this.getPerceel().getWater().getVorigeMeterstand();
            int verbruik = 0;
            int invoer = Integer.parseInt(txtWaterStand.getText());
            
            if (vorigestand == 0) {
                //stand = veerbruik
                verbruik = invoer;
            } else {
                //bereken verbruik
                verbruik = invoer - vorigestand;
            }
            this.getPerceel().getWater().setVorigeMeterstand(invoer);
            this.getPerceel().getWater().setVerbruik(verbruik);
        }
        
        this.setVisible(false);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnSave;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField txtElecStand;
    private javax.swing.JTextField txtElectraNachtstand;
    private javax.swing.JTextField txtGasStand;
    private javax.swing.JTextField txtWaterStand;
    // End of variables declaration//GEN-END:variables

    /**
     * Het perceel wat gewijzigd wordt
     * @return Het perceel
     */
    public Perceel getPerceel() {
        return perceel;
    }

    /**
     * Geeft aan of het formulier geannuleerd is
     * @return True als geannuleerd, anders false
     */
    public boolean isCancel() {
        return _cancel;
    }

}
